package com.example.nio02;

import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/**
 * @Date 2022/10/7
 * @Author Jonathan
 */
public class NIODemo03 {
    public static void main(String[] args) {
        //FileChannel channel = null;
        try (FileChannel channel = FileChannel.open(Paths.get("D:\\code\\example\\netty-learn\\data.txt"), StandardOpenOption.READ)) {
            //channel = FileChannel.open(Paths.get("D:\\code\\example\\netty-learn\\data.txt"), StandardOpenOption.READ);
            final ByteBuffer buffer = ByteBuffer.allocate(10);
            while (true) {
                final int read = channel.read(buffer);
                if (read == -1) {
                    break;
                }
                // 切换为读模式
                buffer.flip();
                while (buffer.hasRemaining()) {
                    final byte b = buffer.get();
                    System.out.print((char) b);
                }
                buffer.clear();
            }
            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
